# this version of getCSPPattern represents the default values out of
# the schoolbook with Donchian Channel Trendfilter

getCSPPattern0 <- function(TS, from="1992", to=NULL) {
  if (!is.xts(TS)) {
    stop("TS argument supplied to getCSPPattern is not a xts object")
  }
  TDChannel <- TrendDetectionChannel(TS)
  
  Hammer <- CSPHammer(TS) & lag(TDChannel,k=1)[,"DownTrend"]
  colnames(Hammer) <- "Hammer_D"
  HangingMan <- CSPHammer(TS) & lag(TDChannel,k=1)[,"UpTrend"]
  colnames(HangingMan) <- "HangingMan_U"
  InvHammer <- CSPInvertedHammer(TS) & lag(TDChannel,k=1)[,"DownTrend"]
  colnames(InvHammer) <- "InvertedHammer_D"
  ShootingStar <- CSPInvertedHammer(TS) & lag(TDChannel,k=1)[,"UpTrend"]
  colnames(ShootingStar) <- "ShootingStar_U"
  Marubozu <- CSPMarubozu(TS)
  WhiteMarubozu1 <- Marubozu[,"WhiteMarubozu"] & lag(TDChannel,k=1)[,"DownTrend"]
  WhiteMarubozu2 <- Marubozu[,"WhiteMarubozu"] & lag(TDChannel,k=1)[,"UpTrend"]
  colnames(WhiteMarubozu1) <- "WhiteMarubozu_D"
  colnames(WhiteMarubozu2) <- "WhiteMarubozu_U"
  BlackMarubozu1 <- Marubozu[,"BlackMarubozu"] & lag(TDChannel,k=1)[,"UpTrend"]
  BlackMarubozu2 <- Marubozu[,"BlackMarubozu"] & lag(TDChannel,k=1)[,"DownTrend"]
  colnames(BlackMarubozu1) <- "BlackMarubozu_U"
  colnames(BlackMarubozu2) <- "BlackMarubozu_D"
  Doji <- CSPDoji(TS)
  # calculate ATR for Doji
  ATRlength <- ATR(cbind(Hi(TS), Lo(TS), Cl(TS)), n=20, maType="SMA")[,"atr"]
  BullDoji <- Doji[,"Doji"] &  lag(TDChannel,k=1)[,"DownTrend"]&
    CandleLength(TS)[,"absCandleLength"] > 0.8*ATRlength
  colnames(BullDoji) <-"BullDoji_D"
  BearDoji <- Doji[,"Doji"] &  lag(TDChannel,k=1)[,"UpTrend"] &
    CandleLength(TS)[,"absCandleLength"] > 0.8*ATRlength
  colnames(BearDoji) <-"BearDoji_U"
  LongDFDoji <- Doji[,"DragonflyDoji"] & 
    lag(TDChannel,k=1)[,"DownTrend"] &
    CandleLength(TS)[,"absCandleLength"] > 0.8*ATRlength
  colnames(LongDFDoji) <- "DragonflyDoji_D"
  LongGSDoji <- Doji[,"GravestoneDoji"] & 
    lag(TDChannel,k=1)[,"UpTrend"]&
    CandleLength(TS)[,"absCandleLength"] > 0.8*ATRlength
  colnames(LongGSDoji) <- "GravestoneDoji_U"
  GAP <- CSPGap(TS)
  GapUp1 <- GAP[,"GapUp"] & lag(TDChannel,k=2)[,"DownTrend"]
  GapUp2 <- GAP[,"GapUp"] & lag(TDChannel,k=2)[,"UpTrend"]
  colnames(GapUp1) <- "GapUp_D"
  colnames(GapUp2) <- "GapUp_U"
  GapDown1 <- GAP[,"GapDown"] & lag(TDChannel,k=2)[,"UpTrend"]
  GapDown2 <- GAP[,"GapDown"] & lag(TDChannel,k=2)[,"DownTrend"]
  colnames(GapDown1) <- "GapDown_U"
  colnames(GapDown2) <- "GapDown_D"
  TasukiGap <- CSPTasukiGap(TS)
  UpTG <- TasukiGap[,"UpsideTasukiGap"] & lag(TDChannel,k=3)[,"UpTrend"]
  colnames(UpTG) <- "UpsideTasukiGap_U"
  DownTG <- TasukiGap[,"DownsideTasukiGap"] & lag(TDChannel,k=3)[,"DownTrend"]
  colnames(DownTG) <- "DownsideTasukiGap_D"
  Engulfing <- CSPEngulfing(TS)
  BullEngulfing <- Engulfing[,"Bull.Engulfing"] & lag(TDChannel,k=2)[,"DownTrend"]
  colnames(BullEngulfing) <- "BullEngulfing_D"
  BearEngulfing <- Engulfing[,"Bear.Engulfing"] & lag(TDChannel,k=2)[,"UpTrend"]
  colnames(BearEngulfing) <- "BearEngulfing_U"
  PiercingPattern <- CSPPiercingPattern(TS) & lag(TDChannel,k=2)[,"DownTrend"]
  colnames(PiercingPattern) <- "PiercingPattern_D"
  DarkCloudCover <- CSPDarkCloudCover(TS) & lag(TDChannel,k=2)[,"UpTrend"]
  colnames(DarkCloudCover) <- "DarkCloudCover_U"
  Stomach <- CSPStomach(TS)
  ATStomach <- Stomach[,"AboveTheStomach"] & lag(TDChannel,k=2)[,"DownTrend"]
  colnames(ATStomach) <- "AboveTheStomach_D"
  BTStomach <- Stomach[,"BelowTheStomach"] & lag(TDChannel,k=2)[,"UpTrend"]
  colnames(BTStomach) <- "BelowTheStomach_U"
  Harami <- CSPHarami(TS)
  BullHarami <- Harami[,"Bull.Harami"] & lag(TDChannel,k=2)[,"DownTrend"]
  colnames(BullHarami) <- "BullHarami_D"
  BearHarami <- Harami[,"Bear.Harami"] & lag(TDChannel,k=2)[,"UpTrend"]
  colnames(BearHarami) <- "BearHarami_U"
  Star <- CSPStar(TS)
  MorningStar <- Star[,"MorningStar"] & lag(TDChannel,k=3)[,"DownTrend"]
  colnames(MorningStar) <- "MorningStar_D"
  EveningStar <- Star[,"EveningStar"] & lag(TDChannel,k=3)[,"UpTrend"]
  colnames(EveningStar) <- "EveningStar_U"
  ThreeWhiteSoldiers <- CSPThreeWhiteSoldiers(TS) & lag(TDChannel,k=3)[,"DownTrend"]
  colnames(ThreeWhiteSoldiers) <- "ThreeWhiteSoldiers_D"
  ThreeBlackCrows <- CSPThreeBlackCrows(TS) & lag(TDChannel,k=3)[,"UpTrend"]
  colnames(ThreeBlackCrows) <- "ThreeBlackCrows_U"

  if (is.null(to)) {
    subsetstring = paste(from, "/", sep="")
  } else {
    subsetstring = paste(from, to, sep="/")
  }
  result <- cbind(
    Hammer,
    HangingMan,
    InvHammer,
    ShootingStar,
    WhiteMarubozu1,
    WhiteMarubozu2,
    BlackMarubozu1,
    BlackMarubozu2,    
    BullDoji,
    BearDoji,
    LongDFDoji,
    LongGSDoji,
    GapUp1,
    GapUp2,
    GapDown1,
    GapDown2,    
    UpTG,
    DownTG,
    BullEngulfing,
    BearEngulfing,
    PiercingPattern,
    DarkCloudCover,
    ATStomach,
    BTStomach,
    BullHarami,
    BearHarami,
    MorningStar,
    EveningStar,
    ThreeWhiteSoldiers,
    ThreeBlackCrows
    )
  result$Random_U <- FALSE
  result$Random_D <- FALSE
  result$Random_U[sample(1:nrow(result),round(nrow(result)/7),replace=FALSE),]<-TRUE
  result$Random_D[sample(1:nrow(result),round(nrow(result)/7),replace=FALSE),]<-TRUE
  result[,"Random_U"] <- result[,"Random_U"] & lag(TDChannel,k=1)[,"UpTrend"]
  result[,"Random_D"] <- result[,"Random_D"] & lag(TDChannel,k=1)[,"DownTrend"]  
  return (result[subsetstring])
}
